package com.css.fxfzxczh.modules.synthesizeInfoManage.vehManage.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.heaManage.entity.HeaManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.vehManage.entity.VehManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.vehManage.queryParams.VehManageQuaParams;
import com.css.fxfzxczh.modules.synthesizeInfoManage.vehManage.repository.VehManageRepository;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class VehManageRepositoryImpl implements VehManageRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Map<String, Object> getPage(VehManageQuaParams queParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from xczh_vehicle_management");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        if (!PlatformObjectUtils.isEmpty(queParams.getDriver())) {
            whereSql.append(" and driver like '%" + queParams.getDriver() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(queParams.getTel())) {
            whereSql.append(" and tel like '%" + queParams.getTel() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(queParams.getVehicleNumber())) {
            whereSql.append(" and vehicle_number like '%" + queParams.getVehicleNumber() + "%'");
        }
        if (!PlatformObjectUtils.isEmpty(queParams.getXsemId())){
            whereSql.append(" and xsem_id = '" + queParams.getXsemId()+ "'");
        }
        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(VehManage.class)).size();
        List<VehManage> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(VehManage.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public VehManage findById(String id) {
        String sql="select * from xczh_vehicle_management where xvm_id = ? ";
        VehManage vehManage = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(VehManage.class), id);
        return vehManage;
    }

    @Override
    public void delete(String id) {
        String sql = "update xczh_vehicle_management set del_flag= ? where xvm_id = ?";
        jdbcTemplate.update(sql, YNEnum.Y.toString(),id);
    }

    @Override
    public void save(VehManage vehManage) {
        String sql="insert into xczh_vehicle_management(xvm_id,xsem_id,vehicle_code,vehicle_number,driver,tel," +
                "comment_info,create_user,create_time,update_user,update_time,del_flag,create_unit,create_user_name,driver_id) " +
                "values(:xvmId, :xsemId, :vehicleCode, :vehicleNumber, :driver, :tel, :commentInfo, :createUser, " +
                ":createTime, :updateUser, :updateTime, :delFlag, :createUnit, :createUserName, :driverId)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(vehManage);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void update(VehManage vehManage) {
        String sql="update xczh_vehicle_management set vehicle_number = :vehicleNumber,driver = :driver,driver_id = :driverId ,tel = :tel," +
                "comment_info = :commentInfo, update_user = :updateUser ,update_time = :updateTime where xvm_id = :xvmId";
        SqlParameterSource source = new BeanPropertySqlParameterSource(vehManage);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public String getMaxNum(String str) {
        String sql="select max(vehicle_code) from xczh_vehicle_management where del_flag = '0' and vehicle_code like '%"+str+"%'";
        String maxParentNum=jdbcTemplate.queryForObject(sql,String.class);
        return maxParentNum;
    }

    @Override
    public void batchSave(List<VehManage> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql="insert into xczh_vehicle_management(xvm_id,xsem_id,vehicle_code,vehicle_number,driver,tel," +
                "comment_info,create_user,create_time,update_user,update_time,del_flag,create_unit,create_user_name,driver_id) " +
                "values(:xvmId, :xsemId, :vehicleCode, :vehicleNumber, :driver, :tel, :commentInfo, :createUser, " +
                ":createTime, :updateUser, :updateTime, :delFlag, :createUnit, :createUserName, :driverId)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }
}
